using System;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.AspNetCore.Authorization;
using Volo.Abp.Data;
using Volo.Abp.Application.Dtos;
using Joy.Erp.BasicArchives.Permissions;
using Joy.Erp.BasicArchives.Core.Filters;

namespace Joy.Erp.BasicArchives.Asns
{
    [Authorize]
    public class AsnTypeAppService : BasicArchivesAppService, IAsnTypeAppService
    {
        protected IAsnTypeRepository AsnTypeRepository => LazyServiceProvider.LazyGetRequiredService<IAsnTypeRepository>();
        protected AsnTypeManager AsnTypeManager => LazyServiceProvider.LazyGetRequiredService<AsnTypeManager>();

        [Authorize(BasicArchivesPermissions.AsnType.Query)]
        public async Task<AsnTypeDto> GetAsync(Guid id)
        {
            return ObjectMapper.Map<AsnType, AsnTypeDto>(
                await AsnTypeRepository.GetAsync(id, true)
            );
        }

        [Authorize(BasicArchivesPermissions.AsnType.Query)]
        public async Task<AsnTypeDto> FindAsync(string filter)
        {
            var entity = await AsnTypeRepository.FindAsync(x => x.DisplayName == filter);
            return ObjectMapper.Map<AsnType, AsnTypeDto>(entity);
        }

        [Authorize(BasicArchivesPermissions.AsnType.Query)]
        public async Task<AsnTypeDto> FindBySourceKeyAsync(string sourceKey, string sourceType)
        {
            var entity = await AsnTypeRepository.FindAsync(x => x.SourceKey == sourceKey && x.SourceType == sourceType);
            return ObjectMapper.Map<AsnType, AsnTypeDto>(entity);
        }

        [Authorize(BasicArchivesPermissions.AsnType.Query)]
        public async Task<PagedResultDto<AsnTypeDto>> GetListAsync(FilterRequestDto input)
        {
            var totalCount = await AsnTypeRepository.GetCountAsync(input.Filter, true);
            var list = await AsnTypeRepository.GetPagedListAsync(input.Filter, input.SkipCount, input.MaxResultCount, input.Sorting, true);

            return new PagedResultDto<AsnTypeDto>(
                totalCount,
                ObjectMapper.Map<List<AsnType>, List<AsnTypeDto>>(list));
        }

        [Authorize(BasicArchivesPermissions.AsnType.Query)]
        public async Task<PagedResultDto<AsnTypeDto>> QueryAsync(FilterGroupRequestDto input)
        {
            Expression<Func<AsnType, bool>> expression = null;
            if (input != null && input.FilterGroup != null)
            {
                expression = FilterConverter.Convert<AsnType>(input.FilterGroup);
            }

            var totalCount = await AsnTypeRepository.GetCountAsync(expression, true);
            var list = await AsnTypeRepository.GetPagedListAsync(expression, input.SkipCount, input.MaxResultCount, input.Sorting, true);

            return new PagedResultDto<AsnTypeDto>(
                totalCount,
                ObjectMapper.Map<List<AsnType>, List<AsnTypeDto>>(list));
        }

        [Authorize(BasicArchivesPermissions.AsnType.Create)]
        public async Task<AsnTypeDto> CreateAsync(AsnTypeCreateDto input)
        {
            var entity = ObjectMapper.Map<AsnTypeCreateDto, AsnType>(input);

            await AsnTypeManager.CreateAsync(entity, true);
            return await GetAsync(entity.Id);
        }

        [Authorize(BasicArchivesPermissions.AsnType.Update)]
        public async Task<AsnTypeDto> UpdateAsync(Guid id, AsnTypeUpdateDto input)
        {
            var entity = await AsnTypeRepository.GetAsync(id);
            entity.SetConcurrencyStampIfNotNull(input.ConcurrencyStamp);

            ObjectMapper.Map(input, entity);

            await AsnTypeManager.UpdateAsync(entity, true);
            return await GetAsync(entity.Id);
        }

        [Authorize(BasicArchivesPermissions.AsnType.Delete)]
        public async Task DeleteAsync(Guid id)
        {
            await AsnTypeManager.DeleteAsync(id);
        }
    }
}